# -*- coding: utf-8 -*-
import scrapy
from bs4 import BeautifulSoup
from ..items import UserSpiderItem
from scrapy_redis.spiders import RedisSpider
import re


class UserspiderSpider(RedisSpider):
    name = 'userSpider'
    allowed_domains = ['weibo.cn']
    baseUrl = 'https://weibo.cn/'

    custom_settings = {
        'ITEM_PIPELINES': {
            'weibo_spiders.pipelines.UserSpiderPipeline': 200,
        },
        'LOG_LEVEL': 'INFO',
        'LOG_STDOUT': True,
        'REDIRECT_ENABLED': False
    }

    def __init__(self, kw=None, *args, **kwargs):
        super(UserspiderSpider, self).__init__(*args, **kwargs)
        self.kw = kw

    # def start_requests(self):
    #     url = self.baseUrl+self.uid+'/info'
    #     yield scrapy.Request(url, callback=self.parse, dont_filter=True)

    def parse(self, response):
        soup = BeautifulSoup(response.text.replace(
            '<br>', '\n').replace('<br/>', '\n'), 'lxml')
        item = UserSpiderItem()
        baseinfo_tag = soup.body.find('div', text='基本信息')
        result = ''
        if baseinfo_tag is not None:
            result = str(baseinfo_tag.next_sibling)

        userid = re.compile(r'https://weibo.cn/.*?/info')
        uid = userid.search(str(response)).group().lstrip(
            'https://weibo.cn/').rstrip('/info')
        print("用户id：{}".format(uid))
        item['user_id'] = uid

        name = re.compile(r'昵称:.+')
        na = name.search(result)

        if(na is not None):
            na = name.search(result).group().lstrip('昵称:')
            item['user_name'] = na

        identification = re.compile(r'认证:.+')
        id = identification.search(result)
        if(id is not None):
            id = identification.search(result).group().lstrip('认证:')
            item['user_identification'] = id

        gender = re.compile(r'性别:.+')
        ge = gender.search(result)
        if(ge is not None):
            ge = gender.search(result).group().lstrip('性别:')
            item['user_gender'] = ge

        location = re.compile(r'地区:.+')
        lo = location.search(result)
        if(lo is not None):
            lo = location.search(result).group().lstrip('地区:')
            item['user_location'] = lo

        birthday = re.compile(r'生日:.+')
        bi = birthday.search(result)
        if(bi is not None):
            bi = birthday.search(result).group().lstrip('生日:')
            item['user_birthday'] = bi

        description = re.compile(r'简介:.+')
        de = description.search(result)
        if(de is not None):
            de = description.search(result).group().lstrip('简介:')
            item['user_description'] = de

        yield item
